﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.IO;
using XWord.Fundamental.Domain;
using XWord.Fundamental.Data;
using XWord.Fundamental.Data.Impl;
using Iesi.Collections;
using System.Collections;
using WordAssistantApp;
using System.Windows.Forms.Integration;
namespace XWord.GUI.Views
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class WordView : Window
    {

        public ObservableCollection<Word> WordList { get; set; }
        public ObservableCollection<string> StartWordList { get; set; }
        private WordRepository wordRepository;
        private EtymaRepository etymaRepository;


        public WordView()
        {
            InitializeComponent();
            wordRepository = new WordRepository();
            etymaRepository = new EtymaRepository();

            //etymaView.AddWord += new EventHandler(etymaView_AddWord);
            //etymaView.TreeSelectedItemChangedEvent += new RoutedPropertyChangedEventHandler<object>(etymaView_TreeSelectedItemChangedEvent);
            //.Where(e=>e.IsIgnore==false)
            //.OrderByDescending(e => e.IsHighFrequency).OrderBy(e => e.IsIgnore)

            //categoryListBox.SelectionChanged += new SelectionChangedEventHandler(categoryListBox_SelectionChanged);
            //etymaListBox.SelectionChanged += new SelectionChangedEventHandler(etymaListBox_SelectionChanged);
            etymaSearchBox.TextChanged += new TextChangedEventHandler(etymaSearchBox_TextChanged);
        }


        void etymaView_TreeSelectedItemChangedEvent(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView element = sender as TreeView;
            Etyma selectItem = element.SelectedItem as Etyma;
            IEnumerable<Word> list = selectItem.Words.OfType<Word>();
            if (list.Count() > 0)
            {
                List<Word> tempWord = new List<Word>();
                wordListBox.Items.Filter = new Predicate<object>((obj) =>
                {
                    var entity = obj as Word;
                    if (list.Any(ee => ee.Id == entity.Id))
                    {
                        tempWord.Add(entity);

                        return true;
                    }
                    // || entity.Name.Contains(selectItem.Name)
                    return list.Any(ee => ee.Id == entity.Id);
                });

                this.Dispatcher.BeginInvoke(new Action(() =>
                {

                    foreach (var item1 in tempWord)
                    {

                        ListBoxItem item = wordListBox.ItemContainerGenerator.ContainerFromItem(item1) as ListBoxItem;
                        if (item != null)
                            item.IsSelected = true;

                        //  item.Background = Brushes.Red;

                    }

                }), System.Windows.Threading.DispatcherPriority.ApplicationIdle);
            }
            else
            {
                wordListBox.Items.Filter = new Predicate<object>((obj) =>
                {
                    var entity = obj as Word;
                    return entity.Name.Contains(selectItem.Name);
                });
            }
        }

        void etymaView_AddWord(object sender, EventArgs e)
        {
            //etymaView.SelectedWords = wordListBox.SelectedItems.Cast<Word>().Select(ee => ee.Id).ToArray();
        }

        void etymaSearchBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            etymaListBox.Items.Filter = new Predicate<object>((obj) =>
            {
                var entity = obj as Etyma;
                return entity.Name.StartsWith(etymaSearchBox.Text);
            });
        }

        void etymaListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            wordListBox.Items.Filter = new Predicate<object>((obj) =>
                {
                    var entity = obj as Word;
                    return entity.Name.Contains((etymaListBox.SelectedItem as Etyma).Name);

                });
        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Window1 win = new Window1();
            //win.ShowDialog();
            //EtymaList = new ObservableCollection<Etyma>(dc.Etymas.OrderBy(ee => ee.Name).ToList());
            //etymaListBox.ItemsSource = EtymaList;
        }

        private void checkBox1_Click(object sender, RoutedEventArgs e)
        {
            if (checkBox1.IsChecked.Value)
            {
                wordListBox.Items.Filter = new Predicate<object>((obj) =>
                {
                    var entity = obj as Word;
                    if (!string.IsNullOrEmpty(textBox1.Text))
                    {
                        return entity.Name.StartsWith(textBox1.Text);
                    }
                    return entity.Name.StartsWith((etymaListBox.SelectedItem as Etyma).Name);
                });
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in wordListBox.SelectedItems)
            {
                var word = item as Word;

                word.IsIgnore = true;
                wordRepository.SaveWord(word);
            }
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {

            var list = new List<string>();
            foreach (var item in wordListBox.SelectedItems)
            {
                var entity = item as Word;

                textBox2.Text += entity.Name + "\n";
                Clipboard.SetDataObject(entity.Name);
            }
        }

        private void button9_Click(object sender, RoutedEventArgs e)
        {
            new EtymaLibraryFinderView().Show();

        }

        private void textBox4_TextChanged(object sender, TextChangedEventArgs e)
        {

             etymaView.DataContext = new ObservableCollection<Etymon>(etymaRepository.GetHierarchicalTree().Where(ee => ee.Name.StartsWith(textBox4.Text)).OrderBy(ee => ee.Name));
        }

        private void categoryListBox_MouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MediaPlayer player=new MediaPlayer();
            Button element=sender as Button;
            Word word= element.DataContext as Word;
            player.Open(new Uri(string.Format("Resources\\{0}.mp3",word.Name),System.UriKind.Relative));
            player.Play();
            //System.Windows.Media
        }

        private void WebBrowser_Loaded(object sender, RoutedEventArgs e)
        {
            WebBrowser wb = sender as WebBrowser;
            Word word=wb.DataContext as Word;
            wb.Source = new Uri(string.Format("http://dict.cn/mini.php?q={0}", word.Name));
        }
    }
}
